package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.heima.common.PageResult;
import com.heima.pojo.VmType;
import com.heima.pojo.dto.VmTypeDto;
import com.heima.pojo.vo.VmTypeVo;
import com.heima.service.IVmTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 售货机类型表
 *
 * @author xjy
 * @since 2024-06-06
 */
@RestController
@RequestMapping("/vm-service/vmType")
@Api(tags = "售货机类型管理")
public class VmTypeController {
    @Autowired
    private IVmTypeService vmTypeService;

    @GetMapping("/search")
    @ApiOperation("分页查询售货机类型")
    public PageResult<VmTypeVo> search(PageResult<VmTypeVo> pageResult, @ApiParam(value = "类型名", required = false) String name) {
        return vmTypeService.search(pageResult, name);
    }

    @PostMapping
    @ApiOperation("添加售货机类型")
    public Boolean add(@RequestBody VmTypeDto vmTypeDto) {
        if (ObjectUtil.isEmpty(vmTypeDto)) {
            return false;
        }
        return vmTypeService.save(BeanUtil.toBean(vmTypeDto, VmType.class));
    }

    @PutMapping("/{typeId}")
    @ApiOperation("修改售货机类型")
    public Boolean update(@PathVariable @ApiParam(value = "类型id", required = true) Integer typeId,
                          @RequestBody @ApiParam(value = "类型信息", required = true) VmTypeDto vmTypeDto) {
        if (ObjectUtil.isEmpty(vmTypeDto)) {
            return false;
        }
        VmType vmType = BeanUtil.toBean(vmTypeDto, VmType.class);
        vmType.setTypeId(typeId);
        return vmTypeService.updateById(vmType);
    }

    @DeleteMapping("/{typeId}")
    @ApiOperation("删除售货机类型")
    public Boolean delete(@PathVariable @ApiParam(value = "类型id", required = true) Integer typeId) {
        if (ObjectUtil.isEmpty(typeId)) {
            return false;
        }
        return vmTypeService.removeById(typeId);
    }
}
